<template>
  <div class="home">
    <el-container>
      <el-header>
        <el-row style="height: 100%;" type="flex" justify="space-between" align="middle">
          <span></span>
          <h1 style="color: #fff;">学生信息管理系统</h1>
          <el-popover size="100" placement="top-start" width="100" trigger="click">
            <div style="text-align: center;">
              <div>{{ userInfo.realName }}</div>
              <el-divider></el-divider>
              <div>
                <el-button type="text">
                  <router-link to="/home/mine" tag="span">个人信息</router-link>
                </el-button>
              </div>
              <div>
                <el-button type="text" @click="loginOut">退出登录</el-button>
              </div>
            </div>
            <template #reference>
              <el-avatar>
                <img v-if="$store.state.userInfo.avatar" :src="'/student/v3' + $store.state.userInfo.avatar">
              </el-avatar>
            </template>
          </el-popover>

        </el-row>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu :default-active="$route.path" background-color="#304156" text-color="#fff" active-text-color="#fff"
            router>
            <el-menu-item index="/home/index">
              <span slot="title">首页</span>
            </el-menu-item>
            <el-menu-item index="/home/classes">
              <span slot="title">班级管理</span>
            </el-menu-item>
            <el-menu-item index="/home/student">
              <span slot="title">学生管理</span>
            </el-menu-item>
          </el-menu></el-aside>
        <el-main>
          <!-- 展示二级路由的内容 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { removeToken } from '@/utils/cookie.js'
export default {
  name: 'Home',
  data() {
    return {
      userInfo: {}
    }
  },
  created() {
    this.$store.dispatch('getUserInfo')
  },
  watch: {
    // 1.异步拿到的，看有没有拿到数据 2.对象形式
    '$store.state.userInfo': {
      immediate: true,
      deep: true,
      handler(newVal) {
        const { createTime, updateTime, userId, ...rest } = newVal
        this.userInfo = JSON.parse(JSON.stringify(rest))
      },
    }
  },
  methods: {
    loginOut() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        type: 'warning'
      }).then(() => {
        // 清空token
        removeToken()
        // 跳转
        this.$router.replace('/login')
      }).catch(() => { })
    }
  }

}
</script>

<style lang="scss" scoped>
.home {
  .el-container {
    height: 100vh;
  }

  .el-header {
    background-color: #304156;
    height: 80px !important;
    padding: 0 100px;
  }

  .el-aside {
    background-color: #304156;

    .is-active {
      background-color: #263445 !important;
    }

    .el-menu {
      border: none;
    }
  }

  .el-main {
    background-color: #fff;


  }

}
</style>
